﻿require.config({
	urlArgs: "version=20170407",
    paths: {
        'jquery': 'packages/jquery',
        'boottree': 'packages/user-treeview',
        'bootstrap': '../bootstrap/js/bootstrap.min'
    },
    shim: {
        'bootstrap': {
            deps: ['jquery'],
            exports: 'bootstrap'
        },
    	'boottree':{
    		deps:['jquery'],
    		exports: 'boottree'
    	}
    }
});

require(['jquery', 'boottree','bootstrap'],function($){
	var perms_list={};
	var roles_list_all={};
	var account_id=0;
	var select_user_id="";
	var account_name="";
	var timeout;
	var user_editRole = false;
	var user_editDisable = false;
	var user_delete = false;
	var treeshow_bool = false;
	$.ajax({
		type : 'POST',
		url : '/users/getId',
		//data : jsonda,
	    cache: false,
		async:false,
		//contentType: "application/json; charset=utf-8",
		dataType : 'json',
		success : function(res) {
			if (res.status == 200) {
					account_id=res.data.id;
					account_name=res.data.name;
					document.getElementById("user-info").innerHTML="&nbsp"+res.data.name;
			} else
				alert("加载失败！");
		},
		error : function(xml, err, throwInfo) {
			alert(err);
		}
    });
	$.ajax({
		type : 'POST',
		url : '/users/getPermList',
		//data : jsonda,
	    cache: false,
		async:false,
		//contentType: "application/json; charset=utf-8",
		dataType : 'json',
		success : function(res) {
			if (res.status == 200) {
				perms_list=res.entries;
			} else
				alert("加载失败！");
		},
		error : function(xml, err, throwInfo) {
			alert(err);
		}
    });
	
	
	
	setPage();
	getTree();
	roles_list_all=getRoleList();
	getUsers();
	//selfresize();
	
	$('#user-info').mouseover(function(){
		document.getElementById("user-info").innerHTML="&nbsp"+"退出登录";
	});
	$('#user-info').mouseleave(function(){
		document.getElementById("user-info").innerHTML="&nbsp"+account_name;
	});
	$('#show-tree').click(function(event){
		var hidebg=document.getElementById('hidebg');
    	if(!treeshow_bool){
    		$('#left-bar')[0].style.display="block";
    		treeshow_bool=true;
    	  	hidebg.style.display="block";
    	}else{
    		$('#left-bar')[0].style.display="";
    		treeshow_bool=false;
    		hidebg.style.display="none";
    	}
    });
	$('#hidebg').click(function(event){
		$('#left-bar')[0].style.display="";
		treeshow_bool=false;
		hidebg.style.display="none";
	});
	$('#user-logout').click(function(){
		$.ajax({
			type : 'GET',
			url : '/logout',
			//data : jsonda,
		    cache: false,
			async:false,
			//contentType: "application/json; charset=utf-8",
			dataType : 'json',
			success : function(res) {
				if (res.status == 200) {
					//循环遍历json，并将其加入显示中
						window.location.href="/login.html";
					
				} else
					alert("退出登录失败！");
			},
			error : function(xml, err, throwInfo) {
				alert(err);
			}
	    });	
	});

	
	
	
	function setPage(){
		
		for(var i in perms_list){
			var perm_list = perms_list[i];
			if(perm_list.Ename=="users：editRole"){
				user_editRole=true;
			}else if(perm_list.Ename=="users：editDisable"){
				user_editDisable=true;
			}else if(perm_list.Ename=="users：delete"){
				user_delete=true;
			}
		}
	}
	function getTree(){
		$('#treeview5').innerHTML="";
		
		var defaultData = [];
		if(!!perms_list){
			for(var i in perms_list){
				var perm_list = perms_list[i];
				if(perm_list.Ename=="admin：selfplib"){
					defaultData.push({
	                	 id:'filelist-h0',
	                	 text: '我的方案',
	                   });
				}
				if(perm_list.Ename=="admin：selfmlib"){
					defaultData.push({
                   		id:'filelist-f0',
		            	text: '我的素材',
	                 });
				}
				if(perm_list.Ename=="admin：mlib"){
					defaultData.push({
                   		id:'filelist-s0',
		            	text: '素材库',
		            	
	                 });
				}
				if(perm_list.Ename=="admin：plib"){
					defaultData.push({
		            	 id:'filelist-t0',
		            	 text: '案例库',
	                 });
				}
				if(perm_list.Ename=="admin：useradmin"){
					defaultData.push({
		                 id:'filelist-0',
		                 text: '用户管理',
		             });
				}
				if(perm_list.Ename=="admin：roleadmin"){
					defaultData.push({
				         id:'filelist-n0',
				         text: '角色管理',
				    });
				}
			}
			defaultData.push({
			      id:'filelist-w0',
			      text: '个人中心',
			 });
		}
		$('#treeview5').treeview({
			 color: "#41464B",
	         selectedBackColor:"#41464B",
	         expandIcon: 'glyphicon glyphicon-chevron-right',
	         collapseIcon: 'glyphicon glyphicon-chevron-down',
	         nodeIcon: 'glyphicon',
	         data:  defaultData
	    });				
	}
	
	
	function getUsers(){
		var users_list={};
		$.ajax({
			type : 'GET',
			url : '/users/list',
		    cache: false,
			async:false,
			contentType: "application/json; charset=utf-8",
			dataType : 'json',
			success : function(res) {
				if (res.status == 200) {
					users_list = res.entries;
				} else
					alert("加载失败！");
			},
			error : function(xml, err, throwInfo) {
				alert(err);
			}
	    });	
		if(!!users_list){
			for(var i in users_list){
				var user_list = users_list[i];
				var state="";
				if(!!!user_list.state){
					state="0";
				}else{
					state="1";
				}
				addUserDiv(user_list.id,user_list.name,user_list.com_name,user_list.com_id,user_list.reg_time,user_list.tele_num,user_list.role,state);
			}
		}
	}
	function getRoleList(){
		var roles_list={};
		$.ajax({
			type : 'GET',
			url : '/roles/list',
		    cache: false,
			async:false,
			contentType: "application/json; charset=utf-8",
			dataType : 'json',
			success : function(res) {
				if (res.status == 200) {
					roles_list = res.entries;
				} else
					alert("加载失败！");
			},
			error : function(xml, err, throwInfo) {
				alert(err);
			}
	    });	
		return roles_list;
		
	}
	function getRoleSelectList(id,role){
		var selectString='';
		if(user_editRole){
			selectString='<select id="changeRole-'+id+'" class="form-control">';
		}else{
			selectString='<select id="changeRole-'+id+'" class="form-control" disabled="disabled">';
		}
		if(!!roles_list_all){
			for(var i in roles_list_all){
				var role_list = roles_list_all[i];

				if('['+role_list.name+']'==role){
					selectString=selectString+'<option value="'+role_list.name+'" selected="selected">'+role_list.name+'</option>';
				}else{
					selectString=selectString+'<option value="'+role_list.name+'">'+role_list.name+'</option>';
				}
			}
			selectString=selectString+'</select>';
		}
		return selectString;
		
	}
	function getDisableSelectList(id,state){
		
		var selectString='';
		if(user_editDisable){
			selectString='<select id="changeState-'+id+'" class="form-control">';
		}else{
			selectString='<select id="changeState-'+id+'" class="form-control" disabled="disabled">';
		}
		if(state=="0"){
			selectString=selectString+'<option value="0" selected="selected">'+'在职'+'</option>';
			selectString=selectString+'<option value="1">'+'离职'+'</option>';
		}else{
			selectString=selectString+'<option value="0">'+'在职'+'</option>';
			selectString=selectString+'<option value="1" selected="selected">'+'离职'+'</option>';
		}
		selectString=selectString+'</select>';
		return selectString;
	}
	
	
	function addUserDiv(id,name,com_name,com_id,reg_time,tele_num,role,state){
		 var tbody = $("table tbody");
		 var newTr = document.createElement('tr');
		 newTr.setAttribute('id',"userlist-"+id);
		 
		 var newTd0 = document.createElement('td');
		 newTd0.innerHTML=id;
		 newTr.appendChild(newTd0);
		 
		 var newTd1 = document.createElement('td');
		 if(!!name){
			 newTd1.innerHTML=name;
		 }else{
			 newTd1.innerHTML="未设定";
		 }
		 newTr.appendChild(newTd1);
		 
		 var newTd2 = document.createElement('td');
		 if(!!name){
			 newTd2.innerHTML=com_name;
		 }else{
			 newTd2.innerHTML="未设定";
		 }
		 newTr.appendChild(newTd2);
		 
		 var newTd3 = document.createElement('td');
		 if(!!name){
			 newTd3.innerHTML=com_id;
		 }else{
			 newTd3.innerHTML="未设定";
		 }
		 newTr.appendChild(newTd3);
		 
		 var newTd4 = document.createElement('td');
		 newTd4.innerHTML=reg_time;
		 newTr.appendChild(newTd4);
		 
		 var newTd5 = document.createElement('td');
		 newTd5.innerHTML=tele_num;
		 newTr.appendChild(newTd5);
		 
		 var newTd6 = document.createElement('td');
		 newTd6.innerHTML=getRoleSelectList(id,role);
		 newTr.appendChild(newTd6);
		 
		 var newTd7 = document.createElement('td');
		 newTd7.innerHTML=getDisableSelectList(id,state)
		 newTr.appendChild(newTd7); 
		 var newTd8 = document.createElement('td');
		 newTd8.setAttribute('id',"delete-user-"+id);
		 newTd8.setAttribute('class',"delete-user");
		 newTd8.innerHTML="删除";
		 if(user_delete){
			 newTr.appendChild(newTd8);	 
		 }
		 $("table tbody").append(newTr);
		 
		 $('#changeState-'+id).change(function () {
			 var newState = $('#changeState-'+id)[0].value;
			 $.ajax({
					type : 'PUT',
					url : '/users/'+id+'/editDisable',
					async:false,
					data : {
						"state":newState
						},
					//timeout:7000,//请求超时后停止请求
					dataType : 'json',
					success : function(res) {
					if (res.status == 200) {
						alert("提交成功!!");
					}else{
						alert("加载失败");
					}},
					error : function(xml, err, throwInfo) {
						alert(err);
					}
				});
		 });
		 
		 $('#changeRole-'+id).change(function () {
				
			 var newName = $('#changeRole-'+id)[0].value;
			 $.ajax({
					type : 'PUT',
					url : '/users/'+id+'/editRole',
					async:false,
					data : {
						"roleName":newName
						},
					//timeout:7000,//请求超时后停止请求
					dataType : 'json',
					success : function(res) {
					if (res.status == 200) {
						alert("提交成功!!");
						 
					}else{
						alert("加载失败");
					}},
					error : function(xml, err, throwInfo) {
						alert(err);
					}
				});
		 });
		 
		 $('#delete-user-'+id).click(function(){
			 select_user_id=id;
			 $('#deleteUser').modal('show');
			 
			 /*
			 $.ajax({
					type : 'DELETE',
					url : '/users/'+id+'/delete',
					async:false,
					dataType : 'json',
					success : function(res) {
					if (res.status == 200) {
						alert("删除成功!!");
						
					}else{
						alert("加载失败");
					}},
					error : function(xml, err, throwInfo) {
						alert(err);
					}
				});
				*/
		 });
	

		 
	}
	$('#delete-user-submit').click(function(){
		 $.ajax({
				type : 'DELETE',
				url : '/users/'+select_user_id+'/delete',
				async:false,
				dataType : 'json',
				success : function(res) {
				if (res.status == 200) {
					alert("删除成功!!");
					$("tbody")[0].removeChild($("#userlist-"+select_user_id)[0]);
				}else if (res.status == 404) {
					alert("在职无法删除!!");
				}else{
					alert("加载失败");
				}},
				error : function(xml, err, throwInfo) {
					alert(err);
				}
			});
		 
	 });
    function selfresize(){
    	//if(window.location.pathname=='/admin/selfmlib.html'){

	  	$('#searchresultall').css('max-height',$(window).height()-$("#term-show").offset().top+'px');	
    	//alert($("#searchresultall").offset().top);
    	if($(window).width()<450){
  
    		$('#function-bar').css('top','0px');
    		$('#toolicon').css('margin-left','8px');
    		$('#user-message').css('margin-right','20px');
    		$('#toolbar').css('margin-right','20px');

    		$('#left-bar').width('98%');
    		$('#left-bar').css('margin-top','30px');
    		$('#term-show').width('98%');
    		
    		if(window.location.pathname=='/user/selfplib.html'){
    			$('#term-show').css('margin-top','70px');
    			$('#toolicon').css('margin-left','8px');
    			$('#searchtext').css('width','90px');
    			$('#searchdiv').css('width','10px');
    			$('#searchbar').css('margin-right','30px');
    			$('#toolicon').css('font-size','8px');
    			$('#topic-mind').css('font-size','25px');
    			
    		}else{
    			$('#term-show').css('margin-top','9px');
    		}
    		
    		$('#term-show').css('min-height',$(window).height()-$("#term-show").offset().top+'px');
    		
    	
    	}else if($(window).width()<697){
    		//alert($(window).width());
    		$('#function-bar').css('top','10px');
    		$('#toolicon').css('margin-left','80px');
    		$('#user-message').css('margin-right','20px');
    		$('#toolbar').css('margin-right','20px');
    		
    		$('#left-bar').width('98%');
    		$('#left-bar').css('margin-top','50px');
    		
    		$('#term-show').width('98%');
    		if(window.location.pathname=='/user/selfplib.html'){
    			$('#term-show').css('margin-top','80px');
    			$('#toolicon').css('margin-left','8px');
    			$('#searchtext').css('width','100px');
    			$('#searchdiv').css('width','10px');
    			$('#searchbar').css('margin-right','50px');
    			$('#topic-mind').css('font-size','35px');
    			
    		}else{
    			$('#term-show').css('margin-top','9px');
    		}
    		
    		$('#term-show').css('min-height',$(window).height()-$("#term-show").offset().top+'px');
    		
    		
    	}else{
    		$('#function-bar').css('top','30px');
    		$('#toolicon').css('margin-left','80px');
    		$('#user-message').css('margin-right','80px');
    		$('#toolbar').css('margin-right','80px');
    		
    		$('#left-bar').width('10%');
    		$('#left-bar').css('margin-top','130px');
    		
    		$('#term-show').width('83%');
    		if(window.location.pathname=='/user/selfplib.html'){
    			$('#searchdiv').css('width','100px');
    			$('#searchtext').css('width','200px');
    			$('#searchbar').css('margin-right','80px');
    			$('#topic-mind').css('font-size','35px');
    		}else{
    			
    		}
    		$('#term-show').css('margin-top','90px');
    		$('#term-show').css('min-height',$(window).height()-$("#term-show").offset().top-50+'px');
    	}
    	//}
    }
});
